<script>
function finalizeApp(stamp,staff_id,appointmentid,customer) {
	if(appointmentid=='') {
		appointmentid="";
	}
	var url = 'finalize-appointment.php';
	if(customer!='') {
		url = 'customer-finalize-appointment.php';
	}
	if(customer!='' && appointmentid!='') {
		return;
	}
	location.href=url+"?stamp="+stamp+"&staff_id="+staff_id+'&appid='+appointmentid;
}
</script>
 <table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">

<?

 
include_once("codelibrary/inc/appointment.class.php"); 
 function getStaffMemebers() {
  	$business_id = $_SESSION['business_id'];
 	$query = "select staff_id,first_name from staff where business_id='$business_id' and status=1";
	$rows = query($query);
	while($staff = mysql_fetch_object($rows) ) {
		$arr['name'][] = $staff->first_name;
		$arr['id'][] = $staff->staff_id;
	}
	
	return $arr;
 }

	 
 $arr = getOfficeHours();
 $timepart = date("Y-m-d",$stamp);

 $start_time = $arr['start_time'];
 $start_time = $timepart." ".date("H:i",$arr['start_time']).":00";
 $start_time = strtotime($start_time);

 $end_time = $arr['end_time'];
 $end_time = $timepart." ".date("H:i",$arr['end_time']).":00";
 $end_time = strtotime($end_time);


 $diff = (getAppointmentTime() * 60);
 
 $staff = getStaffMemebers();


 $app = new appointment($stamp);
	
 $time = $start_time;


 	$offstart = date("Y-m-d",$stamp);
	$offend = date("Y-m-d",$stamp);
	

 	$companyOff = getPureDaysoff($offstart, $offend, "company");
	
	$staffFinal = array();
		
	foreach($staff['id'] as $id) {
		$staffOff = getPureDaysoff($offstart, $offend, "staff",$id);
		for($i=0;$i<count($staffOff);$i++) {
			$staffOff[$i] = $staffOff[$i]."~".$id;
		} 
		$staffFinal = array_merge($staffFinal,$staffOff);
	}

?>
  <tr>
   <td width="50" ><strong>Time</strong></td>
   <? for($i=0;$i<count($staff['name']);$i++) { ?>
   <td bgcolor="#FFFFFF"><strong><?=$staff['name'][$i]?></strong></td>
   <? } ?>
  </tr>
 <? while($time<$end_time) { ?> 
  <tr>
   <td class="apptime"><? $apptime = date("H:i",$time); print $apptime;?></td>
   <? for($i=0;$i<count($staff['name']);$i++) {
   		$ddate = $stamp;
		$reservemsg = "Click here to reserve ".$staff['name'][$i]." at ".date("H:i",$time);
		$appstamp = strtotime(date("Y-m-d",$ddate)." ".$apptime.":00");
   		$db_date = date("Y-m-d",$ddate); 
   		if(in_array($db_date,$companyOff) || in_array(date("w",$ddate),$companyOff)) {
			echo '<td class="unavailable"></td>';
		} else if(in_array($db_date."~".$staff['id'][$i],$staffFinal) || in_array(date("w",$ddate)."~".$staff['id'][$i],$staffFinal)) {
			echo '<td class="unavailable"></td>';
		} else if($app->checkReserved($time,$staff['id'][$i])) {
			?>
			<td style="background-color:<?=$app->statuscolor[$app->currentrow]?>" onclick="finalizeApp(<?=$appstamp?>,<?=$staff['id'][$i]?>,<?=$app->appt_id[$app->currentrow]?>,'<?=$_SESSION['customer_id']?>')">
			
			<? if($_SESSION['customer_id']=='') { ?>
			<span class="toolTipElement" title="Reservation Details::<?=$app->tooltip?>"><img src="images/spacer.gif" width="100%" height="100%"></span>
			<? } ?>
		   
		   </td>
			<?
		}	
		else {
   ?>	   <td  class="available" onclick="finalizeApp(<?=$appstamp?>,<?=$staff['id'][$i]?>,'','<?=$_SESSION['customer_id']?>')">
			<a href="#" title="<?=$reservemsg?>" atl="<?=$reservemsg?>">&nbsp;</a>
		   </td>
		   <? } 
   } ?>
  </tr>
<? 
$time += $diff;
} 
?>

 </table>
